<?php
class model_user extends framework_base_model
{
	
	private static  $table = 'weixin_admin_user';
    /*
     *获取用户列表
     * */
    public static function user_list($pagedata, $condition = array())
    {
		$ex_where = "";
		if(!empty($condition)){
			foreach($condition as $k=>$v){
				$ex_where .= "`{$k}` = '$v' and ";
			}
		}
        $where .= " AND  delete_flag=0 ";
        $sql = "select admin_user_id,user_name,real_name,admin_type,login_count,last_login_ip,last_login_time,status from ".self::$table ." where {$ex_where} delete_flag=0 order by login_count desc ";
        return self::DB()->queryAll($sql,$pagedata);
    }
    /*
     *获取用户总数
     * */
    public static function get_user_count()
    {
   		$sql = "SELECT COUNT(*) AS num FROM ".self::$table." where `delete_flag`=0 ";
    	try{
    		$stmt = self::DB() -> dbh -> prepare($sql);
    		$stmt->execute();
    		return $stmt->fetchColumn();
    	}catch(PDOException $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    }
    /**
     * 通过用户名获取用户信息
     * @param 用户名 $user_name
     */
    public static function get_userinfo_by_username($user_name) {
    	$sql = 'select 
    				admin_user_id,user_name,user_pwd,real_name,raw_add_time,raw_update_time,last_login_ip,last_login_time,login_count,status,admin_type 
    			from '.self::$table .' 
    			where 
    				user_name = :user_name and delete_flag = 0';
    	$data = self::DB()->queryRow($sql ,array(':user_name'=>$user_name));
    	return $data;
    	
    }
    /**
     * 通过用户ID获取用户信息
     * @param 用户ID $user_id
     */
    public static function get_userinfo_byId($user_id) {
    	$sql = 'select 
    				admin_user_id,user_name,user_pwd,real_name,phone,email,part,raw_add_time,raw_update_time,last_login_ip,last_login_time,login_count,status,admin_type 
    			from '.self::$table .' 
    			where 
    				admin_user_id = :admin_user_id and delete_flag = 0';
    	$data = self::DB()->queryRow($sql ,array(':admin_user_id'=>$user_id));
    	return $data;
    	
    }
    /**
     * 新增用户
     * @param array $data
     * @return number
     */
    public  static function add_user($data) {
    	if(!is_array($data)) {
    		return 0;
    	}
    	try{
    		$sql = "INSERT INTO ".self::$table." SET ";
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$sql .= 'raw_add_time=NOW(),raw_update_time=now()';
    		$arr=self::DB() -> dbh -> prepare($sql);
    		$rel = $arr->execute($binds);
    		if($rel == TRUE) {
    			return self::DB()->dbh->lastInsertId();
    		}
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
    	return 0;
    }
    /**
     * 判断是否存在用户名
     * @param 用户名 $user_name
     */
    public static function exists_username($user_name) {
    	$sql = "SELECT COUNT(*) AS num FROM ".self::$table." where `delete_flag`=0 ";
    	$sql .= " and user_name=:user_name";
    	$binds = array(':user_name'=>$user_name);
    	try{
    		$stmt = self::DB() -> dbh -> prepare($sql);
    		$stmt->execute($binds);
    		return $stmt->fetchColumn();
    	}catch(PDOException $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    }
	/*
	* @desc 判断唯一字段
	* @author kenvin wang
	* @param array
	* @return boolean
	*/
	public static function exists_value_key($key, $value){
		$sql = "select count(`admin_user_id`) from `weixin_admin_user` where `{$key}` = :value";
		$binds = array(':value'=>$value);
		try{
    		$stmt = self::DB() -> dbh -> prepare($sql);
    		$stmt->execute($binds);
    		return $stmt->fetchColumn();
    	}catch(PDOException $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
	}
    /*
     * 更新用户基本信息
     * */
    public static function update_user_info($user_id,$data){
    	try{
    		$sql = 'UPDATE '.self::$table.' SET ';
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$ids = implode(',',$user_id);
    		$sql = trim($sql,',');
    		$sql .= ' WHERE `admin_user_id` in ('.$ids.')';
    		$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute($binds);
    		$rowCount = $stm -> rowCount();
    		return $rowCount;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
    	return 0;
    }
    /**
     * 删除用户
     * @param $user_id 用户ID
     */
    
    public static function delete_user($user_id) {
        try{
    		$sql = 'UPDATE '.self::$table.' SET delete_flag = 1';
    		$binds = array();    		
    		$binds[':admin_user_id'] = $user_id;
    		$sql .= ' WHERE `admin_user_id`=:admin_user_id';
    		$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute($binds);
    		$rowCount = $stm -> rowCount();
    		return $rowCount;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
    	return 0;
    }
	/*
	* @desc 获取用户分类权限
	* @author kevin wang
	* @param void
	* @return array
	**/
	public static function get_all_auth(){
		$sql = "select `authority_id` as id,`parent_id` as parentid,`authority_name`,`depth` from `weixin_authority` where `delete_flag` = 0";
		try{
    		$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute();
    		$result = $stm -> fetchAll();
    		return $result;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
	}
	/*
	* @desc 写入用户权限
	* @author kevin wang
	* @param arry
	* @return boolean
	*/
	public static function update_user_auth($admin_id, $authes){
		if(empty($admin_id)){
			return false;
		}
		$sql1 = "update `weixin_admin_auth` set `delete_flag` = :delete_flag where `admin_id` = :admin_id";
		$sql2 = "update `weixin_admin_auth` set `delete_flag` = :delete_flag where `admin_id` = :admin_id and `auth_id` = :auth_id";
		$sql3 = "insert into `weixin_admin_auth` (`admin_id`, `auth_id`, `raw_add_time`) values (:admin_id, :auth_id, NOW())";
		try{
			$pdo = self::DB() -> dbh;
			$pdo->beginTransaction();
    		$stm1 = $pdo -> prepare($sql1);
			$stm2 = $pdo -> prepare($sql2);
			$stm3 = $pdo -> prepare($sql3);
			$stm1->execute(array(':delete_flag' => 1, ':admin_id' => $admin_id));
			foreach($authes as $v){
				$stm2->execute(array(':delete_flag' => 0, ':admin_id' => $admin_id, ':auth_id' => $v));
				$rowCount = $stm2 -> rowCount();
				if($rowCount <= 0){
					$stm3->execute(array(':admin_id'=>$admin_id,':auth_id'=>$v));
				}
			}
			$pdo->commit();
    		return true;
    	}catch(Exception $e){
			$pdo->rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	}
	/*
	* @desc 获取用户全部权限
	* @author kevin wang
	* @param void
	* @return array
	*/
	public static function get_user_authes($admin_id){
		$sql = "select a.`auth_id`,b.`parent_id`,b.`ctrl_tag` from `weixin_admin_auth` as a,`weixin_authority` as b where a.`auth_id` = b.`authority_id` and a.`admin_id` = :admin_id and a.`delete_flag` = 0";
		try{
			$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute(array(':admin_id' => $admin_id));
    		$result = $stm -> fetchAll();
    		return $result;
		}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	}
	/*
	* @desc 权限名称
	* @author kevin wang
	* @param void
	* @return array
	*/
	public static function get_auth_name($ctrl_tag){
		$sql = "select `authority_name` from `weixin_authority` where `ctrl_tag` = :ctrl_tag and `delete_flag` = 0";
		try{
			$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute(array(':ctrl_tag' => $ctrl_tag));
    		$result = $stm -> fetchColumn();
    		return $result;
		}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	}
}

?>